package com.controller;

import com.anno.BackLogin;
import com.bugs.pojo.InfoTypeOption;
import com.bugs.pojo.SpiderOption;
import com.controller.base.BaseController;
import com.controller.base.Response;
import com.model.WorkBug;
import com.service.WorkBugService;
import com.utils.ModelMapperUtils;
import com.utils.from.WorkBugForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 目前只考虑单机环境, 反正我没钱部署分布式, 分布式架构需要一些中间件来记录/通知调度信息
 */
@RestController
@RequestMapping("workBug")
public class WorkBugController extends BaseController {

    @Autowired
    private WorkBugService workBugService;

    @PostMapping("/test")
    public String test() {
        return "test";
    }

    @BackLogin
    @PostMapping("/getOption")
    public Response getOption() {
        Map result = new LinkedHashMap<>(2);
        result.put("spiders", SpiderOption.getOptions());
        result.put("types", InfoTypeOption.getOptions());
        return success(result);
    }

    @BackLogin
    @PostMapping("/list")
    public Response<List<WorkBug>> list() {
        return success(workBugService.findAll());
    }

    @BackLogin
    @PostMapping("/getById")
    public Response<WorkBug> getById(Long id) {
        WorkBug workBug = workBugService.getById(id);
        return success(workBug);
    }

    @BackLogin
    @PostMapping("/save")
    public Response save(@Valid WorkBugForm form) {
        WorkBug workBug = ModelMapperUtils.map(form, WorkBug.class);
        workBugService.save(workBug);
        return success();
    }

    @BackLogin
    @PostMapping("/update")
    public Response update(@Valid WorkBugForm form) {
        WorkBug workBug = ModelMapperUtils.map(form, WorkBug.class);
        workBugService.update(workBug);
        return success();
    }

    @BackLogin
    @PostMapping("/delete")
    public Response delete(Long id) {
        workBugService.deleteById(id);
        return success();
    }

    @BackLogin
    @PostMapping("/action_work")
    public Response actionWork(Long id) {
        workBugService.actionById(id);
        return success();
    }

    @BackLogin
    @PostMapping("/action_suspend")
    public Response suspendWork(Long id) {
        workBugService.suspendById(id);
        return success();
    }


}
